﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace Common
{
    public class AjaxResult
    {
        /// <summary>
        /// 操作结果类型
        /// </summary>
        public object state { get; set; }
        /// <summary>
        /// 获取 消息内容
        /// </summary>
        public string message { get; set; }
        /// <summary>
        /// 获取 返回数据
        /// </summary>
        public object data { get; set; }
    }
    /// <summary>
    /// 表示 ajax 操作结果类型的枚举
    /// </summary>
    public enum ResultType
    {
        /// <summary>
        /// 消息结果类型
        /// </summary>
        info,
        /// <summary>
        /// 成功结果类型
        /// </summary>
        success,
        /// <summary>
        /// 警告结果类型
        /// </summary>
        warning,
        /// <summary>
        /// 异常结果类型
        /// </summary>
        error
    }

    public class CommonFun
    {
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        public static string MD5Encrypt(string strText)
        {
            //MD5 md5 = new MD5CryptoServiceProvider();
            //byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
            //return System.Text.Encoding.Default.GetString(result);
            byte[] result = Encoding.Default.GetBytes(strText.Trim());    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", ""); 
        }

        public static bool IsMobile(string s)
        {
             Regex _mobileregex = new Regex("^(13|15|18)[0-9]{9}$");
            if (string.IsNullOrEmpty(s))

                return true;

            return _mobileregex.IsMatch(s);

        }

        public static string NetxtString( Random random, int size, bool lowerCase)
        {
            StringBuilder _builder = new StringBuilder(size);
            int _startChar = lowerCase ? 97 : 65;//65 = A / 97 = a
            for (int i = 0; i < size; i++)
                _builder.Append((char)(26 * random.NextDouble() + _startChar));
            return _builder.ToString();
        }

        public static string GetTempByID(string tempID)
        {

            string Token = string.Empty;
            // 读取XML文件中的数据，并显示出来 ，注意文件路径
            string filepath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/MsgTemple.xml";

            StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
            XmlDocument xml = new XmlDocument();
            xml.Load(str);
            str.Close();
            str.Dispose();
            XmlNodeList  list = xml.SelectSingleNode("xml").SelectNodes("temp");
            for (int i = 0; i < list.Count; i++)
            {
                if (list.Item(i).SelectSingleNode("tempid").InnerText == tempID)
                {
                    return list.Item(i).SelectSingleNode("tempcontent").InnerText;
                }
            }
            return "";
        }
        public static string GettempSendIDByID(string tempID)
        {

            string Token = string.Empty;
            // 读取XML文件中的数据，并显示出来 ，注意文件路径
            string filepath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/MsgTemple.xml";

            StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
            XmlDocument xml = new XmlDocument();
            xml.Load(str);
            str.Close();
            str.Dispose();
            XmlNodeList list = xml.SelectSingleNode("xml").SelectNodes("temp");
            for (int i = 0; i < list.Count; i++)
            {
                if (list.Item(i).SelectSingleNode("tempid").InnerText == tempID)
                {
                    return list.Item(i).SelectSingleNode("tempSendid").InnerText;
                }
            }
            return "";
        }
        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
        public static string Number(int Length, bool Sleep)
        {
            if (Sleep) System.Threading.Thread.Sleep(3);
            string result = "";
            System.Random random = new Random();
            for (int i = 0; i < Length; i++)
            {
                result += random.Next(10).ToString();
            }
            return result;
        }

        public  static bool IsGuidByReg(string strSrc)
        {
            try
            {
                Guid guid = new Guid(strSrc);
                return true;
            }
            catch
            {
                return false;
            }
            //Regex reg = new Regex("^[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}$", RegexOptions.Compiled);
            //bool c = reg.IsMatch(strSrc);
            //log.Error("------------------------");
            //log.Error(c);
            //log.Error("------------------------");
            //return  c;
        }
    }

    public class WXApi
    {
        public string access_token { set; get; }
    }
    public class WXApiJsTaken
    {
        public string ticket { set; get; }
    }  
}
